package com.test.ai.controller;

import com.test.ai.dto.ChatRequest;
import com.test.ai.service.QwenChatService;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

/**
 * Created with IntelliJ IDEA.
 *
 * @description:
 * @author: liuziyang
 * @since: 2025/8/18 14:23
 * @modifiedBy:
 * @version: 1.0
 */
@RestController
@RequestMapping("/ai")
public class AiController {
  @Autowired
  @Qualifier("openAiChatClient")
  private ChatClient chatClient;

  @Autowired private QwenChatService qwenChatService;

  @GetMapping("/chat")
  public String chat(@RequestParam(value = "msg", defaultValue = "你是谁") String msg) {
    return chatClient.prompt().user(msg).call().content();
  }

  @GetMapping(value = "/stream", produces = "text/html;charset=UTF-8")
  public Flux<String> stream(@RequestParam(value = "msg", defaultValue = "你是谁") String msg) {
    return chatClient.prompt().user(msg).stream().content();
  }

  @PostMapping(value = "/stream/chat", produces = "text/html;charset=UTF-8")
  public Flux<String> chatStream(@RequestBody ChatRequest request) {
    return qwenChatService.generateResponse(request.message(), request.conversationId());
  }
}
